package cn.itcast.goods.cart.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.book.domain.Book;
import cn.itcast.goods.cart.domain.CartItem;
import cn.itcast.goods.cart.service.CartItemService;
import cn.itcast.goods.user.domain.User;
import cn.itcast.servlet.BaseServlet;

public class CartItemServlet extends BaseServlet {

	private CartItemService cartItemService = new CartItemService();
	
	
	/**我的购物车
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String myCart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//1.获取session中的user,并购物的user的uid
		User user = (User) request.getSession().getAttribute("sessionUser");
		String uid = user.getUid();
		//2.调用service中的myCart(String uid)方法，获取当前用户的所有购物车条目
		List<CartItem> cartItemList = cartItemService.mycart(uid);
		
		//3.保存到request,转发到/jsps/cart/list.jsp
		request.setAttribute("cartItemList", cartItemList);
	    return "/jsps/cart/list.jsp";

	}

	/**
	 * 添加到购物车中
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		//1.封装表单中的bid到Book对象中
		Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
		//2.从session中获取当前用户
		User owner = (User) request.getSession().getAttribute("sessionUser");
		//3.把表单中的quantity封装到CartItem中
		CartItem cartItem = CommonUtils.toBean(request.getParameterMap(),CartItem.class);
		//4.设置CartItem 的id
		  //设置book
		  //设置owner
		cartItem.setCartitemid(CommonUtils.uuid());
		cartItem.setBook(book);
		cartItem.setOwner(owner);
		//5.把cartItem添加到数据库中
		cartItemService.add(cartItem);
		//6.调用mycart(),即查询到所有的条目，保存到request中，转发到/jsps/cart/list.jsp
		
		return myCart(request, response);
	}
	/**
	 * 删除购物车中的条目
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        String cartItemIds = request.getParameter("cartItemIds");
        System.out.println(cartItemIds);
        cartItemService.deleteBatch(cartItemIds);
      
		return myCart(request, response);
	
	}
	
	/**
	 * 修改条目的数量
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateQuantity(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.获取cartItenId和quantity
		System.out.println("=====================");
		System.out.println(request.getParameter("cartItemId"));
		String cartItemId = request.getParameter("cartItemId");
		int quantity = Integer.parseInt(request.getParameter("quantity"));
		//2.调用cartItemService # updateQuantity()方法修改数量
		cartItemService.updateQuantity(cartItemId,quantity);
		//3.通过cartItemId 加载CartItem对象
		CartItem cartItem = cartItemService.load(cartItemId);
		//4.把cartItem对象的quantity和subtotal封装成json对象返回
		String result = "{\"quantity\":"+quantity+",\"subtotal:\":"+cartItem.getSubtotal()+"}";
		response.getWriter().print(result);
		return null;
	}
	/**
	 * 显示条目
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadCartItems(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		String cartItemIds = request.getParameter("cartItemIds");
		List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds);
		request.setAttribute("cartItemList", cartItemList);
		request.setAttribute("cartItemIds", cartItemIds);
		return "/jsps/cart/showitem.jsp";
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	}

	
}
